<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 投票池
 * Class Vote
 *
 * @package App\Models
 * @property int $id
 * @property string $name 名称
 * @property string $intro 说明
 * @property string $type 类型(正选反选)
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 * @property int $poll_rater
 * @property int $poll_min
 * @property int $status
 * @property int $user_id
 * @property integer $poll_max
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Works[] $works
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Rater[] $raters
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\VoteDetail[] $details
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Vote whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Vote whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Vote whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Vote whereName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Vote whereIntro($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Vote whereType($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Vote whereDeletedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Vote whereStatus($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Vote wherePollMax($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Vote wherePollMin($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Vote whereUserId($value)
 * @mixin \Eloquent
 */
class Vote extends Model
{
    use SoftDeletes;

    /**
     * 投票开启状态
     */
    const STATUS_LIVE = 1;

    /**
     * 投票关闭状态
     */
    const STATUS_END = 2;

    /**
     * 正选投票
     */
    const TYPE_POSITIVE = 1;

    /**
     * 反选投票
     */
    const TYPE_NEGATIVE = 2;

    protected $fillable = ['name', 'intro', 'type', 'poll_rater', 'poll_min' , 'vote_award_type'];

    /**
     * 作品
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function works()
    {
        return $this->belongsToMany('\App\Models\Works', 'vote_works', 'vote_id', 'works_id');
    }




    /**
     * 评委
     * @return $this
     */
    public function raters()
    {
        return $this->belongsToMany(Rater::class, 'vote_rater', 'vote_id', 'rater_id')
            ->withPivot(['poll_max', 'poll_min', 'confirmed']);
    }

    public function pollTop()
    {

    }

    public function getScore()
    {
        if ($this->type == self::TYPE_POSITIVE) {
            return 1;
        } else {
            return -1;
        }
    }

    public function details()
    {
        return $this->hasMany('\App\Models\VoteDetail', 'vote_id');
    }
}
